package com.revenant.driver.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.bm.library.PhotoView;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.google.gson.JsonSyntaxException;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.qmuiteam.qmui.widget.webview.QMUIWebView;
import com.revenant.driver.R;
import com.revenant.driver.adapter.AlertInfoListAdapter;
import com.revenant.driver.adapter.Driver.SearchEmptyAdapter;
import com.revenant.driver.adapter.PhoneSelectAdapter;
import com.revenant.driver.adapter.SelectCarNumberNewBeanAdapter;
import com.revenant.driver.base.BaseActivity;
import com.revenant.driver.bean.Driver.DriverGoodsLianWangBean;
import com.revenant.driver.bean.Driver.DriverGoodsPingTaiBean;
import com.revenant.driver.bean.Driver.EmptyCarBean;
import com.revenant.driver.bean.Driver.LocationChePaiBean;
import com.revenant.driver.bean.OnlinePhoneBean;
import com.revenant.driver.bean.PersonalCommentShowBean;
import com.revenant.driver.bean.SelectCarNewBean;
import com.revenant.driver.bean.XieyiShenChanBean;
import com.revenant.driver.bean.selectcarnumber.StatusNormalSecondBean;
import com.revenant.driver.bean.selectcarnumber.StatusTabbeddNormalBean;
import com.revenant.driver.utils.GsonUtil;
import com.revenant.driver.utils.SPUtils;
import com.revenant.driver.utils.Utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.revenant.driver.utils.Constants.Comment_PerSonal_Show;
import static com.revenant.driver.utils.Constants.Driver_JustOrder_Order_Second;
import static com.revenant.driver.utils.Constants.Driver_JustOrder_Order_Second_New;
import static com.revenant.driver.utils.Constants.Driver_JustOrder_Order_Second_New_0825;
import static com.revenant.driver.utils.Constants.Driver_Qiangdan;
import static com.revenant.driver.utils.Constants.Driver_Qiangdan_New;
import static com.revenant.driver.utils.Constants.Driver_Qiangdan_New0825;
import static com.revenant.driver.utils.Constants.Driver_ZhiPai_Detail;
import static com.revenant.driver.utils.Constants.Look_LianWang_Detail;
import static com.revenant.driver.utils.Constants.Look_PingTai_Detail;
import static com.revenant.driver.utils.Constants.Name_header_token;
import static com.revenant.driver.utils.Constants.Pop_HeTong;

public class DriverGoodsDetailActivity extends BaseActivity {

    TextView bigStart;
    TextView bigEnd;

    TextView carType;
    TextView carWeight;
    @BindView(R.id.car_pho)
    TextView carPho;
    TextView stratPlace;
    TextView endPlace;
    TextView carName;
    @BindView(R.id.show_driver_info)
    LinearLayout showDriverInfo;
    @BindView(R.id.driver_info_click)
    TextView driverInfoClick;
    Button justqiangdan;
    TextView goodstime;
    @BindView(R.id.show_detail)
    LinearLayout showDetail;
    TextView goodsName;
    @BindView(R.id.car_type_net)
    TextView carTypeNet;
    @BindView(R.id.car_weight_info)
    TextView carWeightInfo;
    LinearLayout goodsNameShow;
    ImageView shippername;
    TextView jiaoyi;
    TextView pingjia;
    TextView shipperscore;
    TextView commentscore;
    TextView singleType;
    TextView singlePrice;
    @BindView(R.id.location)
    ImageView location;
    @BindView(R.id.huowuxinxi)
    TextView huowuxinxi;
    @BindView(R.id.bangdanone)
    TextView bangdanone;
    @BindView(R.id.bangdanoneimage)
    ImageView bangdanoneimage;
    @BindView(R.id.bangdantwo)
    TextView bangdantwo;
    @BindView(R.id.bangdantwoimage)
    ImageView bangdantwoimage;
    @BindView(R.id.bangdanthree)
    TextView bangdanthree;
    @BindView(R.id.bangdanthreeimage)
    ImageView bangdanthreeimage;
    @BindView(R.id.show_bangdan)
    LinearLayout showBangdan;
    @BindView(R.id.photview)
    PhotoView photview;
    @BindView(R.id.showsinglepice)
    LinearLayout showsinglepice;
    @BindView(R.id.show_new_old)
    RelativeLayout showNewOld;
    @BindView(R.id.show_new_new)
    CardView showNewNew;
    TextView luhao;
    @BindView(R.id.callphone)
    TextView callphone;
    @BindView(R.id.show_mobile)
    LinearLayout showMobile;
    @BindView(R.id.show_luhao_lay)
    LinearLayout showLuhaoLay;
    @BindView(R.id.show_yugu_lay)
    LinearLayout showYuguLay;
    @BindView(R.id.show_online)
    LinearLayout showOnline;
    @BindView(R.id.show_lianwang_phone)
    Button showLianwangPhone;
    TextView yugujia;
    @BindView(R.id.order_image_new)
    ImageView orderImageNew;
    @BindView(R.id.new_order_name)
    TextView newOrderName;
    @BindView(R.id.new_order_click)
    TextView newOrderClick;
    @BindView(R.id.dealnumebr)
    TextView dealnumebr;
    @BindView(R.id.delivergoods)
    TextView delivergoods;
    @BindView(R.id.favorable)
    TextView favorable;
    private List<String> titleList = Arrays.asList("平台货源详情", "指派货源详情", "联网货源详情");
    private List<String> urlList = Arrays.asList(Look_PingTai_Detail, Driver_ZhiPai_Detail, Look_LianWang_Detail);
    private int type = 0;
    private String data = "0";
    private SearchEmptyAdapter mSearchEmptyAdapter;
    private int selectone = -1;
    private Context mContext;
    private AlertDialog alertDialog;
    private int driver_id = 0;
    private boolean isOnline = false;
    private String phonenmu = "";
    private String weburl = "";
    private Context context;
    private String Name_header;
    private PhoneSelectAdapter phoneSelectAdapter;

    private List<OnlinePhoneBean> phoneBeanList = new ArrayList<>();

    private String hetongshippername;
    private String hetongdrivername;

    private int isPop = 0;


    private ArrayList<MultiItemEntity> list;
    private SelectCarNumberNewBeanAdapter carnumberadapter;


    private AlertInfoListAdapter alertInfoListAdapter;

    private PopupWindow popstart;
    private AlertDialog alertbuquanDialog;
    private String carnum = "";
    private int typeauto=1;

    @Override
    public int getContentViewResId() {
        return R.layout.activity_goods_detail_old;
    }

    @Override
    public void initView() {
        yugujia = findViewById(R.id.yugujia);
        goodsNameShow = findViewById(R.id.goods_name_show);

        goodsName = findViewById(R.id.goods_name);
        bigStart = findViewById(R.id.big_start);
        bigEnd = findViewById(R.id.big_end);
        carType = findViewById(R.id.car_type);
        carWeight = findViewById(R.id.car_weight);
        goodstime = findViewById(R.id.goodstime);
        stratPlace = findViewById(R.id.strat_place);
        endPlace = findViewById(R.id.end_place);
        carName = findViewById(R.id.car_name);
        luhao = findViewById(R.id.luhao);

        singleType = findViewById(R.id.single_type);
        singlePrice = findViewById(R.id.single_price);

        showMobile = findViewById(R.id.show_mobile);

        callphone = findViewById(R.id.callphone);

        justqiangdan = findViewById(R.id.justqiangdan);
        shippername = findViewById(R.id.shippername);


        shipperscore = findViewById(R.id.shipperscore);
        jiaoyi = findViewById(R.id.jiaoyi);
        pingjia = findViewById(R.id.pingjia);
        commentscore = findViewById(R.id.commentscore);


        mContext = this;
        Bundle bundle = getIntent().getExtras(); //得到传过来的bundle

        data = bundle.getString("goods_id");

        type = bundle.getInt("goods_type", 0);
        typeauto = bundle.getInt("goods_auto", 1);

        getXieyi(SPUtils.getAccounId(this), 2, data);

        setcenterTitle(titleList.get(type));
        getGoodsDetail(data);
        justqiangdan.setVisibility(View.VISIBLE);
        mSearchEmptyAdapter = new SearchEmptyAdapter();
        Name_header = Utils.get64String() + "_" + "&&1";


    }

    List<String> danwei = Arrays.asList("吨", "方", "米", "件");

    private void getGoodsDetail(String dataid) {
        OkGo.<String>get(urlList.get(type))

                .params("id", dataid)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200 && response.body() != null) {
                            if (!response.body().isEmpty()) {
                                switch (type) {
                                    case 0:
                                        DriverGoodsPingTaiBean detailBean =
                                                GsonUtil.parseJsonWithGson(response.body(), DriverGoodsPingTaiBean.class);
                                        if (detailBean.getCode().equals("0")) {
                                            goodsNameShow.setVisibility(View.VISIBLE);
                                            goodsName.setText(detailBean.getData().getName());
                                            bigStart.setText(detailBean.getData().getLoading());
                                            bigEnd.setText(detailBean.getData().getUnload());
                                            carType.setText(detailBean.getData().getVehicle_leader());
                                            carWeight.setText(String.valueOf(detailBean.getData().getWeight()) + danwei.get(detailBean.getData().getDan_wei() - 1));
                                            goodstime.setText(String.valueOf(detailBean.getData().getLoad_date()));
                                            stratPlace.setText(detailBean.getData().getLoading_detail());
                                            endPlace.setText(detailBean.getData().getUnloading_detail());
                                            carName.setText(detailBean.getData().getUsername());
                                            if (detailBean.getData().getRoadConsumption() == null) {
                                                luhao.setText("0");
                                            } else {
                                                luhao.setText(detailBean.getData().getRoadConsumption());
                                            }
                                            driver_id = detailBean.getData().getAccount_id();
                                            List<String> danwei = Arrays.asList("吨", "方", "米", "件");
                                            singleType.setText("每" + danwei.get(Integer.valueOf(detailBean.getData().getDan_wei()) - 1) + "价格");
//                                    BigDecimal bDecimal = new BigDecimal(Double.valueOf(detailBean.getData().getAmount()) / Double.valueOf(detailBean.getData().getWeight()));
                                            singlePrice.setText(detailBean.getData().getPrice());
                                            getScore(driver_id);
                                            isOnline = detailBean.getData().isOnline();
                                            phonenmu = detailBean.getData().getMobile();
                                            if (phonenmu != null) {
                                                if (phonenmu.length() > 0 && phonenmu.length() == 11) {
                                                    showMobile.setVisibility(View.VISIBLE);
                                                    StringBuilder stringBuilder = new StringBuilder(phonenmu);
                                                    stringBuilder.replace(3, 7, "***");
                                                    callphone.setText(stringBuilder.toString());
                                                }
                                            }
                                            if (isOnline) {
                                                justqiangdan.setText("拨打电话");
                                            } else {
                                                justqiangdan.setText("立即抢单");
                                            }
                                            if (detailBean.getData().getAmount() != null) {
                                                yugujia.setText(detailBean.getData().getAmount());
                                            }
                                        }
                                        break;
                                    case 1:
                                        DriverGoodsPingTaiBean detailBeanzhipai =
                                                GsonUtil.parseJsonWithGson(response.body(), DriverGoodsPingTaiBean.class);
                                        if (detailBeanzhipai.getCode().equals("0")) {
                                            if (detailBeanzhipai.getData() != null) {
                                                if (detailBeanzhipai.getData().getAmount() != null) {
                                                    yugujia.setText(String.valueOf(detailBeanzhipai.getData().getAmount()));
                                                }
                                                goodsNameShow.setVisibility(View.VISIBLE);
                                                goodsName.setText(detailBeanzhipai.getData().getName());
                                                bigStart.setText(detailBeanzhipai.getData().getLoading());
                                                bigEnd.setText(detailBeanzhipai.getData().getUnload());
                                                carType.setText(detailBeanzhipai.getData().getVehicle_leader());
                                                carWeight.setText(detailBeanzhipai.getData().getWeight() + danwei.get(detailBeanzhipai.getData().getDan_wei() - 1));
                                                goodstime.setText(String.valueOf(detailBeanzhipai.getData().getLoad_date()));
                                                stratPlace.setText(detailBeanzhipai.getData().getLoading_detail());
                                                endPlace.setText(detailBeanzhipai.getData().getUnloading_detail());
                                                carName.setText(detailBeanzhipai.getData().getUsername());
                                                driver_id = detailBeanzhipai.getData().getAccount_id();
                                                if (detailBeanzhipai.getData().getRoadConsumption() == null) {
                                                    luhao.setText("0");
                                                } else {
                                                    luhao.setText(detailBeanzhipai.getData().getRoadConsumption());
                                                }
                                                List<String> danwei = Arrays.asList("吨", "方", "米", "件");
                                                singleType.setText("每" + danwei.get(Integer.valueOf(detailBeanzhipai.getData().getDan_wei()) - 1) + "价格");
//                                        BigDecimal bDecimal = new BigDecimal(Double.valueOf(detailBeanzhipai.getData().getAmount()) / Double.valueOf(detailBeanzhipai.getData().getWeight()));
                                                singlePrice.setText(String.valueOf(detailBeanzhipai.getData().getPrice()));
                                                phonenmu = detailBeanzhipai.getData().getMobile();
                                                if (phonenmu != null) {
                                                    if (phonenmu.length() > 0 && phonenmu.length() == 11) {
                                                        showMobile.setVisibility(View.VISIBLE);
                                                        StringBuilder stringBuilder = new StringBuilder(phonenmu);
                                                        stringBuilder.replace(3, 7, "***");
                                                        callphone.setText(stringBuilder.toString());
                                                    }
                                                }
                                                getScore(driver_id);
                                                isOnline = detailBeanzhipai.getData().isOnline();
                                                phonenmu = detailBeanzhipai.getData().getMobile();
                                                if (isOnline) {
                                                    justqiangdan.setText("拨打电话");
                                                } else {
                                                    justqiangdan.setText("立即抢单");
                                                }
                                            }

                                        }
                                        break;
                                    case 2:
                                        DriverGoodsLianWangBean detailBeanlianwang =
                                                GsonUtil.parseJsonWithGson(response.body(), DriverGoodsLianWangBean.class);
                                        if (detailBeanlianwang.getCode().equals("0")) {
                                            bigStart.setText(
                                                    detailBeanlianwang.getData().getSetoutcity() +
                                                            " " +
                                                            detailBeanlianwang.getData().getSetoutcounty());
                                            bigEnd.setText(detailBeanlianwang.getData().getDestinationcity() + " " + detailBeanlianwang.getData().getDestcounty());
                                            if (detailBeanlianwang.getData().getCtype().isEmpty() && detailBeanlianwang.getData().getCweight().isEmpty()) {
                                                carTypeNet.setVisibility(View.GONE);
                                                carType.setVisibility(View.GONE);
                                            }
                                            carType.setText(detailBeanlianwang.getData().getCtype().isEmpty() ? detailBeanlianwang.getData().getCweight() + "吨 " + detailBeanlianwang.getData().getTlength() + " " + detailBeanlianwang.getData().getTtype() + " " : detailBeanlianwang.getData().getCtype() + " " + detailBeanlianwang.getData().getCweight() + "吨 " + detailBeanlianwang.getData().getTlength() + " " + detailBeanlianwang.getData().getTtype());
                                            carWeightInfo.setText("货源信息");
                                            carWeight.setText(detailBeanlianwang.getData().getInfo());
                                            goodstime.setText(detailBeanlianwang.getData().getSendtime() + (detailBeanlianwang.getData().getUpdatetime() == null ? "" : "-" + detailBeanlianwang.getData().getUpdatetime()));

                                            stratPlace.setText(detailBeanlianwang.getData().getSetoutprovince() + detailBeanlianwang.getData().getSetoutcity() + " " + detailBeanlianwang.getData().getSetoutcounty());

                                            endPlace.setText(detailBeanlianwang.getData().getDestinationprovince() + detailBeanlianwang.getData().getDestinationcity() + " " + detailBeanlianwang.getData().getDestcounty());

                                            justqiangdan.setVisibility(View.GONE);
                                            showsinglepice.setVisibility(View.GONE);
                                            showLuhaoLay.setVisibility(View.GONE);
                                            showYuguLay.setVisibility(View.GONE);
                                            showOnline.setVisibility(View.GONE);
                                            showLianwangPhone.setVisibility(View.VISIBLE);

                                            String phonenumber = detailBeanlianwang.getData().getPhonenumber();
                                            if (phonenumber != null && !phonenumber.isEmpty()) {
                                                String[] split = phonenumber.split(" ");
                                                for (int i = 0; i < split.length; i++) {
                                                    OnlinePhoneBean onlinePhoneBean = new OnlinePhoneBean();
                                                    onlinePhoneBean.setPhonenum(split[i]);
                                                    phoneBeanList.add(onlinePhoneBean);
                                                }
                                            }
                                        }
                                        break;

                                }
                            }
                        }


                    }
                });
    }

    @Override
    public void initData() {
//        showDialog(weburl);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.justqiangdan, R.id.show_mobile, R.id.show_lianwang_phone})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.justqiangdan:
                if (isOnline) {
                    if (phonenmu.trim().isEmpty()) {
                        showToast("电话为空");
                    } else {
                        callPhone(phonenmu.trim());
                    }
                } else {
                    showDig();
                    selectone = -1;
                    carnum = "";
//                    getDriverEmpty(data);
                    selectcarnumber();
                }

                break;
            case R.id.driver_info_click:
                if (driver_id != 0) {
                    Bundle bundlea = new Bundle();
                    bundlea.putString("driverid", String.valueOf(driver_id));
                    startActivity(DriverInfoActivity.class, bundlea);
                }
                break;
            case R.id.show_mobile:
                if (showMobile.getVisibility() == View.VISIBLE) {
                    if (phonenmu.trim().isEmpty()) {
                        showToast("电话为空");
                    } else {
                        callPhone(phonenmu.trim());
                    }
                }
                break;

            case R.id.show_lianwang_phone:
                if (phoneBeanList.size() > 0) {
                    getDriverEmpty(phoneBeanList);
                } else {
                    showToast("该用户没有发布联系电话");
                }
                break;
        }
    }

    private void getDriverSelect(String goodsId, String carNo, String driverId,int carDriverId) {
        OkGo.<String>post(Driver_Qiangdan_New0825)
                .headers(Name_header_token, Name_header)
                .params("goodsId", goodsId)
                .params("carNo", carNo)
                .params("driverId", driverId)
                .params("carDriverId", carDriverId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        dismissDig();
                        if (response.code() == 200 && response.body() != null) {
                            if (!response.body().isEmpty()) {
                                EmptyCarBean g = GsonUtil.parseJsonWithGson(response.body(), EmptyCarBean.class);
                                if (g != null) {
                                    if (g.getCode().equals("0")) {
                                        showToast(g.getMsg());
                                        alertDialog.dismiss();
                                        if (typeauto==2){
                                           startActivity(DriverOrderActivity.class);
                                        }

                                        finish();
                                    } else {
                                        showToast(g.getMsg());
                                    }
                                }

                            }
                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDig();
                    }
                });

    }

    @Override
    public void leftbarclick() {
        super.leftbarclick();
        finish();
    }

    private void getDriverEmpty(String id) {
        OkGo.<String>get(Driver_JustOrder_Order_Second)

                .params("id", SPUtils.getAccounId(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        dismissDig();
                        LocationChePaiBean g = GsonUtil.parseJsonWithGson(response.body(), LocationChePaiBean.class);
                        if (g.getCode().equals("0")) {

                            if (g.getData().size() > 0) {
                                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                                View view = getLayoutInflater().inflate(R.layout.alert_dialog_search_empty, null);
                                RecyclerView type = view.findViewById(R.id.recyclerview);
                                ImageView cancel = view.findViewById(R.id.cancel);
                                Button button = view.findViewById(R.id.justqiangdan);
                                mSearchEmptyAdapter = new SearchEmptyAdapter();
                                mSearchEmptyAdapter.setNewData(g.getData());
                                type.setLayoutManager(new LinearLayoutManager(mContext));
                                type.setAdapter(mSearchEmptyAdapter);
                                mSearchEmptyAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                    @Override
                                    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                        switch (view.getId()) {
                                            case R.id.select:
                                                if (selectone == -1) {
                                                    mSearchEmptyAdapter.getData().get(position).setIsselect(true);
                                                    mSearchEmptyAdapter.notifyItemChanged(position);
                                                    selectone = position;

                                                } else if (selectone != position) {
                                                    mSearchEmptyAdapter.getData().get(selectone).setIsselect(false);
                                                    mSearchEmptyAdapter.notifyItemChanged(selectone);
                                                    mSearchEmptyAdapter.getData().get(position).setIsselect(true);
                                                    mSearchEmptyAdapter.notifyItemChanged(position);
                                                    selectone = position;
                                                }
                                                break;
                                        }
                                    }
                                });
                                builder.setView(view);
                                alertDialog = builder.show();

                                final Window window = alertDialog.getWindow();
//                                window.setBackgroundDrawable(new ColorDrawable(0));
                                cancel.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        alertDialog.dismiss();

                                    }
                                });

                                button.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (selectone == -1) {
                                            showToast("请选择车辆");
                                        } else {

                                            showXieyi();

//
                                        }
                                    }
                                });
                            } else if (g.getCode().equals("-1")) {
                                showToast(g.getMsg());
                                finish();
                                startActivity(DriverVehicleManageActivity.class);
                            } else {
                                showToast("车辆目前为空,请添加车辆");
                                finish();
                                startActivity(DriverVehicleManageActivity.class);
                            }

                        } else if (g.getCode().equals("-1")) {
                            showToast(g.getMsg());
                            finish();
                            startActivity(DriverVehicleManageActivity.class);

                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDig();
                    }
                });

    }

    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }

    private void getScore(int id) {
        OkGo.<String>get(Comment_PerSonal_Show)

                .params("id", id)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            if (response.body() != null) {
                                if (!response.body().isEmpty()) {
                                    PersonalCommentShowBean personal = GsonUtil.parseJsonWithGson(response.body(), PersonalCommentShowBean.class);
                                    if (Integer.valueOf(personal.getCode()) == 0) {

                                        Glide.with(getApplicationContext()).load(personal.getData().getPhoto()).placeholder(R.mipmap.picture).into(shippername);
                                        shipperscore.setText(personal.getData().getRate());
                                        jiaoyi.setText(String.valueOf(personal.getData().getOrderCount()));
                                        pingjia.setText(String.valueOf(String.valueOf(personal.getData().getEvaluateCount())));
                                        commentscore.setText(String.valueOf(String.valueOf(personal.getData().getScore())));
                                    }
                                }
                            }


                        }

                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        showToast(response.message());
                    }
                });
    }

    private void showDialog(String url) {
        QMUIDialog.CustomDialogBuilder dialogBuilder = new QMUIDialog.CustomDialogBuilder(this);
        dialogBuilder.setLayout(R.layout.dialog_xieyi);
        final QMUIDialog dialog = dialogBuilder.setTitle("货物运输协议")
                .addAction("不同意", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "同意", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        lijiqiangdan();
                    }
                })
                .create();

        TextView textStart = (TextView) dialog.findViewById(R.id.title_start_text);
        TextView endStart = (TextView) dialog.findViewById(R.id.title_end_text);


        QMUIWebView displayImageView = (QMUIWebView) dialog.findViewById(R.id.webxieyi);
        ImageView webxieyiimage = (ImageView) dialog.findViewById(R.id.webxieyiimage);
        displayImageView.getSettings().setBuiltInZoomControls(true);
        displayImageView.getSettings().setLoadWithOverviewMode(true);
        displayImageView.getSettings().setUseWideViewPort(true);
        if (!url.isEmpty()) {
            displayImageView.loadUrl(url);
        } else {
            displayImageView.setVisibility(View.GONE);
            webxieyiimage.setVisibility(View.VISIBLE);
            webxieyiimage.setImageResource(R.mipmap.xieyi);
        }

        if (hetongshippername != null) {
            if (hetongshippername.length() > 0) {
                textStart.setText("甲方:" + hetongshippername);
            }
        }

        if (hetongdrivername != null) {
            if (hetongdrivername.length() > 0) {

                endStart.setText("乙方:" + hetongdrivername);
            }
        }


        dialog.show();
    }

    private void getXieyi(int accountId, int type, String goodsId) {
        OkGo.<String>get(Pop_HeTong)
                .params("accountId", accountId)
                .params("type", type)
                .params("goodsId", goodsId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        if (response.code() == 200) {
                            if (response.body() != null) {
                                XieyiShenChanBean webUrlBean = GsonUtil.parseJsonWithGson(response.body(), XieyiShenChanBean.class);
                                if (webUrlBean.getCode().contains("0")) {
                                    if (webUrlBean.getData().getUrl() != null) {
                                        if (!webUrlBean.getData().getUrl().isEmpty()) {
                                            weburl = webUrlBean.getData().getUrl();
                                            isPop = webUrlBean.getData().getIsPop();
                                            hetongshippername = webUrlBean.getData().getShipperName();
                                            hetongdrivername = webUrlBean.getData().getDriverName();
                                        }
                                    }


                                }
                            }
                        }


                    }
                });
    }


    private void getDriverEmpty(List<OnlinePhoneBean> phoneBeanList) {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = getLayoutInflater().inflate(R.layout.phone_select, null);
        RecyclerView type = view.findViewById(R.id.recyclerview);
        ImageView cancel = view.findViewById(R.id.cancel);

        phoneSelectAdapter = new PhoneSelectAdapter();
        phoneSelectAdapter.setNewData(phoneBeanList);
        type.setLayoutManager(new LinearLayoutManager(this));
        type.setAdapter(phoneSelectAdapter);

        builder.setView(view);
        final AlertDialog alertDialog = builder.show();
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });
        phoneSelectAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.click:
                        alertDialog.dismiss();
                        callPhone(phoneSelectAdapter.getData().get(position).getPhonenum());
                        break;

                }
            }
        });
    }

    private void showXieyi() {
        int buquanguanche = 1;
        int carbuquanid = -1;
        String carbuquannum = "";

        try {
            StatusNormalSecondBean secondBean = (StatusNormalSecondBean) carnumberadapter.getData().get(selectone);
            if (secondBean.isConsummate == 0) {
                buquanguanche = 0;
                carbuquanid = secondBean.carDriverId;
                carbuquannum = secondBean.cardNumber;
            }


        } catch (ClassCastException ex) {
            showToast("数据异常,返回到上一页面");
        }

        if (selectone == -1) {
            showToast("请选择空闲车辆");
        } else if (buquanguanche == 0) {
            alertDialog(carbuquannum, carbuquanid);


        } else {
            if (isPop == 1) {
                if (!weburl.isEmpty()) {
                    showDialog(weburl);
                } else {
                    showDialog(weburl);
                }

            } else {
                lijiqiangdan();
            }
        }

    }


    private void lijiqiangdan() {
        showDig();

        if (carnumberadapter.getData() != null) {

            /*请选择空闲车辆*/
            if (selectone == -1) {
                showToast("请选择空闲车辆");
            } else {
                try {

                    StatusNormalSecondBean secondBean = (StatusNormalSecondBean) carnumberadapter.getData().get(selectone);
                    if (secondBean.cardNumber != null) {
                        if (!secondBean.cardNumber.isEmpty()) {
                            getDriverSelect(data, secondBean.cardNumber, String.valueOf(SPUtils.getAccounId(this)),secondBean.carDriverId);
                        }

                    }


                } catch (ClassCastException ex) {
                    showToast("数据异常,返回到上一页面");
                }
            }


        }


//        getDriverSelect(data, mSearchEmptyAdapter.getData().get(selectone).getCarNo(),String.valueOf(SPUtils.getAccounId(this)));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Glide.with(getApplicationContext()).pauseRequests();
        if (alertDialog != null) {
            alertDialog.dismiss();
        }
    }


    private void selectcarnumber() {
        OkGo.<String>get(Driver_JustOrder_Order_Second_New_0825)

                .params("id", SPUtils.getAccounId(this))
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        dismissDig();

                        try {
                            SelectCarNewBean g = GsonUtil.parseJsonWithGson(response.body(), SelectCarNewBean.class);
                            if (g.getCode().equals("0")) {

                                if (g.getData() != null) {
                                    if (g.getData().size() > 0) {
                                        if (list != null) {
                                            list.clear();
                                        } else {
                                            list = new ArrayList<>();
                                        }
                                        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                                        View view = getLayoutInflater().inflate(R.layout.chepaiselect, null);
                                        RecyclerView type = view.findViewById(R.id.recyclerview);
                                        TextView cancel = view.findViewById(R.id.cancel);
                                        TextView ok = view.findViewById(R.id.confirm);
                                        cancel.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                alertDialog.dismiss();
                                            }
                                        });

                                        ok.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                alertDialog.dismiss();

                                                if (list != null) {
                                                    if (selectone == -1) {
                                                        showToast("请选择车辆");
                                                    } else {
                                                        showXieyi();

                                                    }
                                                } else {
                                                    finish();
                                                    showToast("车辆目前为空,请添加车辆");
                                                    startActivity(DriverEmptyCarManageActivity.class);
                                                }


                                            }
                                        });
                                        if (g.getData() != null) {

                                            for (int i = 0; i < g.getData().size(); i++) {

                                                StatusNormalSecondBean secondBean = new StatusNormalSecondBean(
                                                        g.getData().get(i).getCarNo(),
                                                        g.getData().get(i).getCarId(),
                                                        g.getData().get(i).getStatus(),
                                                        g.getData().get(i).getIsConsummate(),
                                                        g.getData().get(i).getCarDriverId(),
                                                        g.getData().get(i).getIsTrailerCarInfo()
                                                );


                                                if (g.getData().get(i).getStatus() == 0) {
                                                    StatusTabbeddNormalBean statusNormalBean = new StatusTabbeddNormalBean(g.getData().get(i).getCarInfo(), g.getData().get(i).getStatus(),g.getData().get(i).getIsTrailerCarInfo());
                                                    secondBean.addSubItem(statusNormalBean);
                                                    list.add(secondBean);

                                                } else {

                                                    StatusTabbeddNormalBean statusNormalBean = new StatusTabbeddNormalBean(
                                                            g.getData().get(i).getCarInfo(),
                                                            g.getData().get(i).getCarStatus().getDriverPhoto(),
                                                            g.getData().get(i).getCarStatus().getDriverName(),
                                                            g.getData().get(i).getCarStatus().getOrderCtime(),
                                                            g.getData().get(i).getCarStatus().getDriverMobile(),
                                                            g.getData().get(i).getStatus(),
                                                            g.getData().get(i).getIsConsummate(),
                                                            g.getData().get(i).getCarDriverId(),
                                                            g.getData().get(i).getIsTrailerCarInfo()


                                                    );

                                                    secondBean.addSubItem(statusNormalBean);
                                                    list.add(secondBean);


                                                }

                                            }


                                        }

                                        carnumberadapter = new SelectCarNumberNewBeanAdapter(list);


                                        final GridLayoutManager manager = new GridLayoutManager(context, 1);


                                        type.setLayoutManager(manager);
                                        type.setAdapter(carnumberadapter);


                                        carnumberadapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                            @Override
                                            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                                switch (view.getId()) {
                                                    case R.id.check_select:
                                                        if (selectone == -1) {
                                                            StatusNormalSecondBean multiItemEntity = (StatusNormalSecondBean) carnumberadapter.getData().get(position);
                                                            multiItemEntity.setIsselect(true);
                                                            carnumberadapter.notifyItemChanged(position);
                                                            selectone = position;

                                                            /*carnum车辆显示*/

                                                        } else if (selectone != position) {
                                                            StatusNormalSecondBean multiItemEntity = (StatusNormalSecondBean) carnumberadapter.getData().get(selectone);
                                                            multiItemEntity.setIsselect(false);
                                                            carnumberadapter.notifyItemChanged(selectone);
                                                            StatusNormalSecondBean multiItemEntityposition = (StatusNormalSecondBean) carnumberadapter.getData().get(position);
                                                            multiItemEntityposition.setIsselect(true);
                                                            carnumberadapter.notifyItemChanged(position);
                                                            selectone = position;
                                                        } else if (selectone == position) {
                                                            StatusNormalSecondBean multiItemEntity = (StatusNormalSecondBean) carnumberadapter.getData().get(selectone);
                                                            if (multiItemEntity.isIsselect()) {
                                                                selectone = -1;
                                                            } else {
                                                                selectone = position;
                                                            }
                                                            multiItemEntity.setIsselect(multiItemEntity.isIsselect());
                                                            carnumberadapter.notifyItemChanged(selectone);
                                                        }
                                                        break;
                                                    case R.id.callphone:
                                                        StatusTabbeddNormalBean statusTabbeddNormalBean = (StatusTabbeddNormalBean) carnumberadapter.getData().get(position);
                                                        if (!statusTabbeddNormalBean.phone.isEmpty()) {
                                                            Utils.callPhone(DriverGoodsDetailActivity.this, statusTabbeddNormalBean.phone);
                                                        }
                                                        break;

                                                }
                                            }
                                        });


//                                carnumberadapter.expandAll();

                                        builder.setView(view);
                                        alertDialog = builder.show();


                                    } else {
                                        showToast("请到车辆管理添加车辆,才可以抢单");
                                        finish();
                                        startActivity(DriverVehicleManageActivity.class);

                                    }
                                }


                            } else if (g.getCode().equals("-1")) {
                                showToast(g.getMsg());
                                finish();
                                startActivity(DriverVehicleManageActivity.class);
                            } else {
                                showToast(g.getMsg());
                            }
                        } catch (JsonSyntaxException e) {
                            showToast("返回数据异常,请稍后再试");
                        }


                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        dismissDig();
                    }
                });
    }


    private void alertDialog(String carnum, int cardid) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        View contentView = View.inflate(this, R.layout.alertbuquanguache, null);
        ImageView imageclose = (ImageView) contentView.findViewById(R.id.right_close);
        LinearLayout gobuquan = (LinearLayout) contentView.findViewById(R.id.buquanguache);
        gobuquan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /**/
                Bundle bundlecar = new Bundle();
                bundlecar.putInt("cartype", 0);
                bundlecar.putInt("cartbuqanId", cardid);
                startActivity(BuQuanGuaCheActivity.class, bundlecar);

                alertbuquanDialog.dismiss();

                finish();
            }
        });
        TextView car_num_info = (TextView) contentView.findViewById(R.id.car_num_info);
        car_num_info.setText(carnum);


        imageclose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertbuquanDialog.dismiss();

            }
        });

        builder.setView(contentView);
        alertbuquanDialog = builder.show();


    }

    private void darkenBackground(Float alpha) {
        if (alpha < 0 || alpha > 1) {
            return;
        }
        WindowManager.LayoutParams windowLP = getWindow().getAttributes();
        windowLP.alpha = alpha;
        if (alpha == 1) {
            //不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            //此行代码主要是解决在华为手机上半透明效果无效的bug
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        getWindow().setAttributes(windowLP);

    }
}

